﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BOL;
using System.Data.Linq;
using System.Data.SqlClient;
using Website.Logger;
using UtilityExtensions;
namespace UILGestion.AlmacenAdmin
{
    public partial class NotaSalidaListForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                try
                {

                    idObra.Value = (string)Request.QueryString["idObra"];
                    idAlmacen.Value = (string)Request.QueryString["idAlmacen"];

                    menuObraCtrl1.loadMenuAlmacen(int.Parse(idObra.Value), int.Parse(idAlmacen.Value)); 
                    FormatForm();


                }
                catch (SqlException sqlex)
                {
                    WebServerSideSqlError.Log(sqlex);
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


                }
                catch (Exception ex)
                {
                    WebServerSideError.Log(ex);
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

                }


            }
        }

        private void Find()
        {
            //dbGestionDataContext _db;

            //_db = new dbGestionDataContext(Parametros.strConnString);

            ////(DD/MM/yyyy)
            //System.IFormatProvider MiFp =
            //                                            new System.Globalization.
            //                                            CultureInfo("es-ES", false);
            //valores de parametro

            //int selectid =  int.Parse(idObra.Value);

            //valores de parametro

            //DateTime fechaInicio = new DateTime(2010, 1, 1);
            //DateTime fechafin = DateTime.Now;

           

            //if (inicioDateTime.Text.Length > 0)
            //{
            //    fechaInicio =
            //        System.DateTime.ParseExact(inicioDateTime.Text, "dd/MM/yyyy", MiFp);
            //}
            //if (finDateTime.Text.Length > 0)
            //{
            //    fechafin =
            //        System.DateTime.ParseExact(finDateTime.Text, "dd/MM/yyyy", MiFp);
            //}

            //NotaSalidaBL recBol = new NotaSalidaBL();

            //List<NotaSalidaListInfo> e = 
            //    recBol.GetNotaSalidaList(int.Parse(idObra.Value));
            
            //entityListGridView.DataSource = e.ToList();
            //entityListGridView.DataBind();

            

        }

        private void FormatForm()
        {
            ShowAllComboBox();


            inicioDateTime.Text =
              DbUtil.Db.Obras.Single(p => p.idInternal == int.Parse(idObra.Value)).fechaInicio.Value.ToShortDateString();
            finDateTime.Text = DateTime.Now.ToShortDateString();
            //restricionesForm();
            //Find();
            //estadistica();

         
        }
        protected void ShowAllComboBox()
        {

            ListValueBL bl= new ListValueBL();
            
            motivoDDL.DataTextField="descripcion";
            motivoDDL.DataValueField="idInternal";

            ListValueInfo todos = new ListValueInfo();
            todos.descripcion = "-----TODOS------";
            todos.idInternal=0;

            var lista = bl.GetMotivoNotaSalida();
            lista.Insert(0, todos);


            motivoDDL.DataSource = lista;
            motivoDDL.DataBind();

          

        }


        protected void nuevoButton_Click(object sender, EventArgs e)
        {

            try
            {
                //segun su Almacen
                Obra obra = DbUtil.Db.Obras.Single(p => p.idInternal == idObra.Value.ToInt());

                if (obra.Almacens.Count == 0)
                {
                    throw new Exception("No Existe almacen");
                }

                Response.Redirect("NotaSalidaInsertForm.aspx?type=A&idAlmacen={0}&idObra={1}".Fmt(idAlmacen.Value, idObra.Value), true);
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
           
        }


        protected void obraDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
               // Find();
                //estadistica();
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void fechaInicioDateTime_TextChanged(object sender, EventArgs e)
        {
            try
            {
               // Find();

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }


        protected void actualizarButton_Click(object sender, EventArgs e)
        {
            try
            {
              //  FormatForm();
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }

        protected void BuscarBtn_Click(object sender, EventArgs e)
        {
            NotaSalidaListGrid1.DataBind();
        }
   
    }
}